home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
- From: Dan Pop <danpop@mail.cern.ch>
- Newsgroups: comp.lang.c
- Subject: Re: quick decision: is n a power of 2?
- Date: Sun, 21 Jan 1996 16:09:28 +0100
- Organization: CERN European Lab for Particle Physics
- Message-ID: <9601211509.AA01185@dxmint.cern.ch>
- References: <4dpd94$c25@fountain.mindlink.net> <4dsaco$a39@newsbf02.news.aol.com> <9601211103.AA19325@dxmint.cern.ch> <31022d1e.215811776@nntp.ix.netcom.com>
- X-NNTP-Posting-Host: hpl3sn03.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
- X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
-
- miker3@ix.netcom.com (Mike Rubenstein) writes:
-
- >> babycox@aol.com (BabyCox) writes:
- >>
- >> >Here is a revision, this should work (but that's what I said last time)
- >>
- >> You're a little bit closer this time :-)
- >>
- >> >Boolean isPowerOfTwo(long x)
- >> >{
- >> > long tst;
- >> > for(tst=1<<31;tst!=0;tst>>=1)
- >> > if (x==tst) return true;
- >> >
- >> > return false;
- >> >}
- >>
- >There's also the problem that it will fail if long is more than 32
- >bits and x is greater than pow(2, 32).
- ^
- or equal
-
- The original poster asked for a solution for 32-bit unsigned long's so
- x cannot be greater than pow(2, 32) (or even equal).
-
- The nice thing is that all the fast (i.e. non-iterative) solutions
- don't need to know the size of the argument. When implemented as
- macros, their argument can have any unsigned type.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-